qui(function(require, exports, module) {
    Server.Version = function(options) {
        this.dao = Server.Dao;
        this.options = $.extend(true, {}, options);

        this.util = Server.Util || {};
        this.config = Server.config || {};

    };

    Com.inherits(Server.Version, Ancestor.Grid).extend({
        cVersion:{},
        run: function(options, callback) {
            var self = this;
            var $container = self.options.$container;
            var gridConfig = self.getGridConfig(Server.Config.TABLE_zzdServer,Server.Config.TABLE_VERSION_COLMODEL_CONFIG);

            self.$grid = $container.find('table.grid');
            options = $.extend(true, {
                width: 1600,
                shrinkToFit: false,
                autowidth: false
            }, gridConfig, options);
            self.$grid.jqGrid(options);
            self.initMainVersion();

			self.perpareData();
            self.bindEvent();
        },

        initMainVersion: function(){
            var self = this;
            self.dao.versionHistory({},function(rdata){
                self.cVersion=self.versionList(rdata);
                self.initDialog();

                self.dao.getMainVersion({},function (rrdata) {
                    var vs = rrdata['data'].split(",");
                    var good = 1;
                    for(var i in vs){
                        if(vs[i]==""){
                            good = 0;
                            break;
                        }
                    }
                    if(good==1){
                        self.cVersion['mainVersion']=rrdata['data'].split(",");
                    }else{
                        self.cVersion['mainVersion']=self.cVersion['default']
                    }
                    Server.Util.difMainVersion(self.cVersion['mainVersion']);
                    Server.Util.showDialog(self.cVersion['mainVersion']);
                    Server.Util.closeDialog();
                });
            });
        },

        perpareData: function() {
            var self = this;
            var username = $("div.user a.name").html();
        },

        bindEvent: function() {
            var self = this;
            var $container = self.options.$container;
            var $mdiv = $("#mainVersionDiv");


            $container.find(".query-btn").on('click.query', function() {
                self.query(function(){
                    Server.Util.difMainVersion(self.cVersion['mainVersion']);
                });
                return false;
            });

            $container.find(".main-version").on('click', function() {
                Server.Util.showDialog(self.cVersion['mainVersion']);
            });

            $mdiv.find(".setMain").on('click', function() {
                var $btn=$(this);
                $btn.attr('disabled','disabled').html('提交中..');
                var versions = Server.Util.getNewMainVersion($mdiv);
                self.cVersion['mainVersion'] = versions.split(",");
                self.dao.setMainVersion({'versions':versions},function(){});
                self.dao.synVersionStatus({'versions':versions},function(){
                    self.query(function(){
                        Server.Util.difMainVersion(self.cVersion['mainVersion']);
                        Server.Util.closeDialog();
                        $btn.attr('disabled',false).html('设置');
                    });
                });
                return false;
            });

            $mdiv.find(".cancel").on('click', function() {
                $mdiv.dialog("close");
            });

        },


        versionList: function (rdata) {
            var info = [[],[],[],[],[],[],[]];
            var self = this;
            var resp = rdata['data'];
            var mainVersion = [];
            info[Server.Config.VERSION_IND['zzdVersion']] = resp[Server.Config.VERSION_IND['zzdVersion']].split(',').slice(0,-1);
            info[Server.Config.VERSION_IND['encoderVersion']] = resp[Server.Config.VERSION_IND['encoderVersion']].split(',').slice(0,-1);
            info[Server.Config.VERSION_IND['mergeToolVersion']] = resp[Server.Config.VERSION_IND['mergeToolVersion']].split(',').slice(0,-1);
            info[Server.Config.VERSION_IND['mergeServiceVersion']] = resp[Server.Config.VERSION_IND['mergeServiceVersion']].split(',').slice(0,-1);
            info[Server.Config.VERSION_IND['precisionCutToolVersion']] = resp[Server.Config.VERSION_IND['precisionCutToolVersion']].split(',').slice(0,-1);
            info[Server.Config.VERSION_IND['hardware']] = resp[Server.Config.VERSION_IND['hardware']].split(',').slice(0,-1);
            info[Server.Config.VERSION_IND['serverStatus']] = resp[Server.Config.VERSION_IND['serverStatus']].split(',');
            //info[Server.Config.VERSION_IND['versionStatus']] = resp[Server.Config.VERSION_IND['versionStatus']].split(',');
            
            for (var index in info){
                if(index == Server.Config.VERSION_IND['versionStatus']){
                    //Server.Util.statusList($('select').eq(index),info[index],Server.Config.VERSION_IND['versionStatus']);
                }else if(index == Server.Config.VERSION_IND['serverStatus']){
                    Server.Util.statusList($('select').eq(index),info[index],Server.Config.VERSION_IND['serverStatus']);
                }else{
                    Server.Util.versionHistory($('select').eq(index),info[index]);
                    mainVersion.push(info[index][0]);
                }
            }
            mainVersion.shift();
            info.shift();
            info.shift();
            info.shift();

            return {'versionHist':info,'default':mainVersion};
        },

        getParam: function(pageSize) {
            var self = this;
            var $container = self.options.$container;

            var $grid = self.$grid;
            var params = {};
            var $form = $container.find('form[name="basicFrm"]');

            var $elements = $.makeArray($form.prop('elements')).filter(function(item) {
                return true;
            });

            var checkboxVal = [];
            var name;
            var key;
            $.each($elements, function() {
                name = this.name;
                if (name) {
                    if($(this).val()){
                        params[name] = $.trim($(this).val());
                    }
                }
            });

            if(arguments.length==1){
                return {'paramsObj': JSON.stringify(params),'pageSize':pageSize}
            }

            return {'paramsObj': JSON.stringify(params)};
        },
        query: function(callback) {
            var self = this;
            var $container = self.options.$container;
            var $grid = self.$grid;
            self.overridePostData($grid);
            var params = self.getParam();
            $grid.jqGrid('setGridParam', {
                postData: params,
                gridComplete: function(){
                    callback.call(this);
                }
            }).trigger('reloadGrid', [{
                page: 1
            }]);
        },

        initDialog: function () {
            var self = this;
            $("#mainVersionDiv").dialog({
                title: "主版本设置",
                autoOpen: false,
                width: 400,
                modal: true,
                dialogClass: 'alert',
                resizable: false,
                open: function () {}
            });
            $("#mainVersionDiv").find('select').each(function (i,elem) {
                var v = self.cVersion['versionHist'][i];
                for(var index in v){
                    var node = $("<option>").val(v[index]).html(v[index]);
                    $(elem).append(node);
                }
            });
            $(".ui-dialog-titlebar-close").html('<span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span><span class="ui-button-text">close</span>');
        },

    });

});
